//Connection Strategy
double AutoPlayer::analyseConnect(int x0,int y0,stoneColor color) {
    double Hw = 1, Vw = 1, Lw = 1, Rw = 1, score = 0;
    int x, y,stones,jump,temp;
    int Len = game->getLen();
    stoneColor pos;
    //horizontal
    jump = 0;
    stones = 0;
    y = y0;
    for (x = x0 + 1; x < Len; x++) { //skip the central Pos
        pos = game->getPos(x,y);
        if (pos == color) {
            stones++;
        }
        else if (pos == VACANT) {
            jump++;
            if (jump > 2) {
                jump = 0;
                break;
            }
        }
        else if (pos == 1 - color) {
            break;
        }
    }
    for (x = x0 - 1 ; x >= 0; x--) {
        pos = game->getPos(x,y);
        if (pos == color) {
            stones++;
        }
        else if (pos == VACANT) {
            jump++;
            if (jump > 2) {
                jump = 0;
                break;
            }
        }
        else if (pos == 1 - color) {
            break;
        }
    }
    score += Hw * stones;

    //vertical
    x = x0;
    jump = 0;
    stones = 0;
    for ( y = y0 + 1; y < Len; y++) {
        pos = game->getPos(x,y);
        if (pos == color) {
            stones++;
        }
        else if (pos == VACANT) {
            jump++;
            if (jump > 2) {
                jump = 0;
                break;
            }
        }
        else if (pos == 1 - color) {
            break;
        }
    }
    for (y = y0 - 1 ; y >= 0; y--) {
        pos = game->getPos(x,y);
        if (pos == color) {
            stones++;
        }
        else if (pos == VACANT) {
            jump++;
            if (jump > 2) {
                jump = 0;
                break;
            }
        }
        else if (pos == 1 - color) {
            break;
        }
    }
    score += Vw * stones;

    //left slant
    jump = 0;
    stones = 0;
    for(x = x0 - 1, y = y0 - 1; x >= 0 &&y >= 0; x--,y--) {
        pos = game->getPos(x,y);
        if (pos == color) {
            stones++;
        }
        else if (pos == VACANT) {
            jump++;
            if (jump > 2) {
                jump = 0;
                break;
            }
        }
        else if (pos == 1 - color) {
            break;
        }
    }
    for(x = x0 + 1, y = y0 + 1; x < Len && y < Len; x++,y++) {
        pos = game->getPos(x,y);
        if (pos == color) {
            stones++;
        }
        else if (pos == VACANT) {
            jump++;
            if (jump > 2) {
                jump = 0;
                break;
            }
        }
        else if (pos == 1 - color) {
            break;
        }
    }
    score += Lw * stones;

    //right slant
    jump = 0;
    stones = 0;
    for(x = x0 + 1, y = y0 - 1; x < Len && y >= 0; x++,y--) {
        pos = game->getPos(x,y);
        if (pos == color) {
            stones++;
        }
        else if (pos == VACANT) {
            jump++;
            if (jump > 2) {
                jump = 0;
                break;
            }
        }
        else if (pos == 1 - color) {
            break;
        }
    }
    for(x = x0 - 1, y = y0 + 1; x >= 0 && y < Len; x--,y++) {
        pos = game->getPos(x,y);
        if (pos == color) {
            stones++;
        }
        else if (pos == VACANT) {
            jump++;
            if (jump > 2) {
                jump = 0;
                break;
            }
        }
        else if (pos == 1 - color) {
            break;
        }
    }
    score += Rw * stones;
    return score;
 }

Step AutoPlayer::searchConnect() {
    Step step,lastStep = game->getLastStep();
    step.color = color;
    int Len = game->getLen();
    int x,y;
    double MScore = -10000,attackScore,defendScore,score;
    double attack = 1,defend = 3;
    for (x = 0; x < Len; x++) {
        for (y = 0; y < Len; y++) {
            if (game->getPos(x,y) != VACANT) {
                continue;
            }
            attackScore = analyseConnect(x,y,color);
            defendScore = analyseConnect(x,y,stoneColor(1 - color));
            score = (attack * attackScore + defend * defendScore);
            if ( score > MScore) {
                step.x = x;
                step.y = y;
                MScore = score;
            }
        }
    }
    return step;
}



